package class10;

import class09.LinkedTools.*;

/**
 * <a href="https://leetcode.cn/problems/linked-list-cycle/">141. 环形链表</a>
 * <a href="https://leetcode.cn/problems/linked-list-cycle-ii/">142. 环形链表 II</a>
 * <a href="https://leetcode.cn/problems/intersection-of-two-linked-lists/">160. 相交链表</a>
 */
public class Code01_FindFirstIntersectNode {

    public static class Solution {
        // 141. 环形链表
        public boolean hasCycle(ListNode head) {
            // 两种方法
            // 1 利用哈希表直接判断，额外空间
            // 2 快慢指针，常数空间（√）
            // 判断空链表情况，链表自身为空和只有一个结点的时候无环，直接返回false
            if (head == null || head.next == null) {
                return false;
            }
            // 用两个指针，slow指针每次走一步，fast指针每次走两步
            // 两种情况：
            // 1 fast指针走到null的话，那么链表必定无环
            // 2 fast指针走不到null的话，slow和fast必定会在某一结点相遇
            ListNode slow = head.next;
            ListNode fast = head.next.next;
            while (slow != fast) {
                if (fast == null || fast.next == null) {
                    return false;
                }
                slow = slow.next;
                fast = fast.next.next;
            }
            return true;
        }

        // 142. 环形链表 II
        public ListNode detectCycle(ListNode head) {
            // 两种方法
            // 1 利用哈希表直接判断，额外空间
            // 2 快慢指针，常数空间（√）
            // 判断空链表情况，链表自身为空和只有一个结点的时候无环，直接返回false
            if (head == null || head.next == null) {
                return null;
            }
            // 用两个指针，slow指针每次走一步，fast指针每次走两步
            // 两种情况：
            // 1 fast指针走到null的话，那么链表必定无环
            // 2 fast指针走不到null的话，slow和fast必定会在某一结点相遇
            ListNode slow = head.next;
            ListNode fast = head.next.next;
            while (slow != fast) {
                if (fast == null || fast.next == null) {
                    return null;
                }
                slow = slow.next;
                fast = fast.next.next;
            }
            // 相遇后，将slow指针从头结点开始每次走一步，fast在相遇点每次走一步
            slow = head;
            while (slow != fast) {
                slow = slow.next;
                fast = fast.next;
            }
            // 此时再次相遇就是相交的结点，返回slow或fast都可以
            return slow;
        }

        // 160. 相交链表
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            // pa结点走到尾的时候，那么距离是a-z
            // pb结点走到尾的时候，那么距离是b-z
            // pa走到尾在从headB开始走，那么距离就是 a+(b-z)
            // pb走到尾在从headA开始走，那么距离就是 b+(a-z)
            // 那么就符合结合律 a+(b−z)=b+(a−z)，就可以得出两个结论
            // 1 两链表有公共尾部，pa和pb会在同时指向第一个相交的结点
            // 2 两链表无公共尾部，pa和pb会同时指向null
            ListNode pa = headA;
            ListNode pb = headB;
            while (pa != pb) {
                pa = pa == null ? headB : pa.next;
                pb = pb == null ? headA : pb.next;
            }
            return pa;
        }
    }

    public static ListNode getIntersectNode(ListNode head1, ListNode head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        ListNode loop1 = getLoopNode(head1);
        ListNode loop2 = getLoopNode(head2);
        if (loop1 == null && loop2 == null) {
            return noLoop(head1, head2);
        }
        if (loop1 != null && loop2 != null) {
            return bothLoop(head1, loop1, head2, loop2);
        }
        return null;
    }

    // 找到链表第一个入环节点，如果无环，返回null
    public static ListNode getLoopNode(ListNode head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        // n1 慢  n2 快
        ListNode slow = head.next; // n1 -> slow
        ListNode fast = head.next.next; // n2 -> fast
        while (slow != fast) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            fast = fast.next.next;
            slow = slow.next;
        }
        // slow fast  相遇
        fast = head; // n2 -> walk again from head
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }

    // 如果两个链表都无环，返回第一个相交节点，如果不相交，返回null
    public static ListNode noLoop(ListNode head1, ListNode head2) {
        if (head1 == null || head2 == null) {
            return null;
        }
        ListNode cur1 = head1;
        ListNode cur2 = head2;
        int n = 0;
        while (cur1.next != null) {
            n++;
            cur1 = cur1.next;
        }
        while (cur2.next != null) {
            n--;
            cur2 = cur2.next;
        }
        if (cur1 != cur2) {
            return null;
        }
        // n  :  链表1长度减去链表2长度的值
        cur1 = n > 0 ? head1 : head2; // 谁长，谁的头变成cur1
        cur2 = cur1 == head1 ? head2 : head1; // 谁短，谁的头变成cur2
        n = Math.abs(n);
        while (n != 0) {
            n--;
            cur1 = cur1.next;
        }
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return cur1;
    }

    // 两个有环链表，返回第一个相交节点，如果不想交返回null
    public static ListNode bothLoop(ListNode head1, ListNode loop1, ListNode head2, ListNode loop2) {
        ListNode cur1 = null;
        ListNode cur2 = null;
        if (loop1 == loop2) {
            cur1 = head1;
            cur2 = head2;
            int n = 0;
            while (cur1 != loop1) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {
                n--;
                cur2 = cur2.next;
            }
            cur1 = n > 0 ? head1 : head2;
            cur2 = cur1 == head1 ? head2 : head1;
            n = Math.abs(n);
            while (n != 0) {
                n--;
                cur1 = cur1.next;
            }
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            return cur1;
        } else {
            cur1 = loop1.next;
            while (cur1 != loop1) {
                if (cur1 == loop2) {
                    return loop1;
                }
                cur1 = cur1.next;
            }
            return null;
        }
    }

    public static void main(String[] args) {
        // 1->2->3->4->5->6->7->null
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(2);
        head1.next.next = new ListNode(3);
        head1.next.next.next = new ListNode(4);
        head1.next.next.next.next = new ListNode(5);
        head1.next.next.next.next.next = new ListNode(6);
        head1.next.next.next.next.next.next = new ListNode(7);

        // 0->9->8->6->7->null
        ListNode head2 = new ListNode(0);
        head2.next = new ListNode(9);
        head2.next.next = new ListNode(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new ListNode(1);
        head1.next = new ListNode(2);
        head1.next.next = new ListNode(3);
        head1.next.next.next = new ListNode(4);
        head1.next.next.next.next = new ListNode(5);
        head1.next.next.next.next.next = new ListNode(6);
        head1.next.next.next.next.next.next = new ListNode(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new ListNode(0);
        head2.next = new ListNode(9);
        head2.next.next = new ListNode(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(getIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new ListNode(0);
        head2.next = new ListNode(9);
        head2.next.next = new ListNode(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(getIntersectNode(head1, head2).value);
    }

}